package com.jeesite.modules.digitalize.service;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.entity.vo.system.ComponentMenuVo;
import com.jeesite.modules.digitalize.entity.vo.system.EmployeeVo;
import com.jeesite.modules.digitalize.entity.vo.system.OfficeVo;
import com.jeesite.modules.digitalize.entity.vo.system.ProjectVo;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Menu;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.service.EmployeeService;
import com.jeesite.modules.sys.service.OfficeService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author patronli
 * @Description: 系统数据
 * @date 2023年09月11日 09:16:27
 */
@Service
public class SystemService {

    @Autowired
    private OfficeService officeService;
    @Autowired
    private EmployeeService employeeService;

    public static List<EmployeeVo> EMPLOYEEVOS = new ArrayList<>();

    /**
     * @date 2023年09月11日 16:26:01
     * @author patronli
     * @Description: 查询当前登录人的菜单
     */
    public ComponentMenuVo menus() {
        ComponentMenuVo vo = new ComponentMenuVo();
        List<Menu> meus = UserUtils.getMenuList();
//        Employee employ = EmpUtils.getEmployee();
//        Office off = employ.getOffice();
        List<String> permissions = meus.stream().filter(item -> item.getModuleCodes().equals(Constants.MODULENAME)).map(item -> {
            return item.getPermission();
        }).collect(Collectors.toList());
        vo.setPermissions(permissions);
        return vo;
    }

    /**
     * @date 2023年09月11日 16:27:28
     * @author patronli
     * @Description: 大区项目列表
     */
    public List<ProjectVo> projects() {
        List<ProjectVo> vo = new ArrayList<>();
        List<Office> offices = officeService.findList(new Office());
        List<Office> zones = offices.stream().filter(item -> item.getOfficeType().equals(Constants.ZONE_MARK)).collect(Collectors.toList());//筛选大区
        if (CollectionUtils.isNotEmpty(zones)) {//组装大区信息
            vo = zones.stream().map(item -> {
                return new ProjectVo(item.getOfficeCode(), item.getOfficeName());
            }).collect(Collectors.toList());
        }
        List<Office> projects = offices.stream().filter(item -> item.getOfficeType().equals(Constants.PROJECT_MARK)).collect(Collectors.toList());//筛选项目
        if (CollectionUtils.isNotEmpty(projects) && CollectionUtils.isNotEmpty(zones)) {//组装大区项目的关系
            for (ProjectVo project : vo) {
                List<ProjectVo> projectVo = projects.stream().filter(item -> item.getParentCodes().contains(project.getOfficeCode())).map(item -> {
                    return new ProjectVo(item.getOfficeCode(), item.getOfficeName());
                }).collect(Collectors.toList());
                project.setProjects(projectVo);
            }
        }
        return vo;
    }


    public List<OfficeVo> offices() {
        List<OfficeVo> vo = new ArrayList<>();
        List<Office> offices = officeService.findList(new Office());
        if (CollectionUtils.isNotEmpty(offices)) {
            vo = offices.stream().map(item -> {
                return new OfficeVo(item.getOfficeCode(), item.getParentCode(), item.getOfficeName(), item.getFullName());
            }).collect(Collectors.toList());
            vo = buildDataTree(vo, Constants.DEFAULT_TOP_PARENT_ID);
        }
        return vo;
    }


    public List<EmployeeVo> employees(String officeCode) {
        List<EmployeeVo> vo = new ArrayList<>();
        Employee condition = new Employee();
        Office office = new Office();
        if(StringUtils.isNotEmpty(officeCode)){
            office.setOfficeCode(officeCode);
        }
        condition.setOffice(office);
        List<Employee> employees = employeeService.findList(condition);
        if (CollectionUtils.isNotEmpty(employees)) {
            vo = employees.stream().map(item -> {
                return new EmployeeVo(item.getEmpCode(), item.getEmpName());
            }).collect(Collectors.toList());
        }
        return vo;

    }

    public String getOfficeName(String officeId) {
        String officeName = "未定义";
        if (StringUtils.isNotEmpty(officeId)) {
            Office office = officeService.get(officeId);
            if (office != null) {
                officeName = office.getOfficeName();
            }
        }
        return officeName;
    }


    public List<OfficeVo> buildDataTree(List<OfficeVo> datas, String parentId) {
        List<OfficeVo> source = datas.stream().filter(e -> e.getParentId().equals(parentId)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(source)) {
            return null;
        }
        List<OfficeVo> tree = new ArrayList<>();
        source.forEach(item -> {
            if (item.getParentId().equals(parentId)) {
                List<OfficeVo> children = buildDataTree(datas, item.getOfficeCode());
                tree.add(new OfficeVo(item.getOfficeCode(), item.getParentId(), item.getOfficeName(), item.getFullName(), children));
            }
        });
        return tree;
    }
}
